package com.ezops.controller;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import com.ezops.dto.TradeExceptionTaskDTO;
import com.ezops.model.FieldValidationError;
import com.ezops.models.User;

/**
 * Controller for proving data to all task related functionality.
 * 
 * @author vgaurav
 * 
 */
@Controller
@RequestMapping("/tradetask")
public class TradeExceptionTaskController {

	@Autowired
	private RestTemplate restTemplate;

	@RequestMapping(value = "/getUserTasks", method = RequestMethod.GET, produces = "application/json")
	public @ResponseBody
	List<TradeExceptionTaskDTO> getAllUnclaimedTasks(
			@RequestParam(value = "taskId", required = false, defaultValue = "usertask1") String taskId) {
		ResponseEntity<List> response = restTemplate
				.getForEntity(
						"http://localhost/tradeprocessing/service/ezops/tradetasks",
						List.class);
		List<TradeExceptionTaskDTO> dtos = response.getBody();
		return dtos;
	}

	@RequestMapping(value = "/gettaskdetails/{taskId}", method = RequestMethod.GET, produces = "application/json")
	public String getTaskDetails(Model model,
			@PathVariable(value = "taskId") String taskId) {
		ResponseEntity<List> response = restTemplate.getForEntity(
				"http://localhost/tradeprocessing/service/ezops/taskdata/"
						+ taskId, List.class);
		List<FieldValidationError> dtos = response.getBody();
		// model.addAttribute("message", "Exception Resolution");
		User userObj = (User) SecurityUtils.getSubject().getPrincipal();
		model.addAttribute("user",
				userObj.getFirstName() + " " + userObj.getLastName());
		model.addAttribute("userId", userObj.getId());
		model.addAttribute("pic", userObj.getProfilePic());
		model.addAttribute("fielderrors", dtos);
		return "exceptionresolution";
	}

	public RestTemplate getRestTemplate() {
		return restTemplate;
	}

	public void setRestTemplate(RestTemplate restTemplate) {
		this.restTemplate = restTemplate;
	}

}
